from pdf2image import convert_from_path
import os

def convert_pdf_to_jpg(pdf_file):
    base_name = os.path.splitext(os.path.basename(pdf_file))[0]
    output_folder = os.path.dirname(pdf_file)
    images = convert_from_path(pdf_file)

    for i, image in enumerate(images):
        output_filename = f"{base_name}_{i+1}.jpg"
        output_path = os.path.join(output_folder, output_filename)
        image.save(output_path, "JPEG")

def process_directory(directory, keywords):
    for root, dirs, files in os.walk(directory):
        for file in files:
            if file.endswith('.pdf') and any(keyword in file for keyword in keywords):
                pdf_file_path = os.path.join(root, file)
                convert_pdf_to_jpg(pdf_file_path)

# 使用示例
directory = 'E:\\小象第二批\\zhungeer2.7z\\准格尔金诚2\\5.兰银-5431笔\\兰银-代偿证明、债转协议-齐\\兰银补充\\5217\\2712703007615231139841'  # PDF文件所在的目录
keywords = ['代偿明细','债转明细']  # 只转换文件名中包含这些关键字之一的PDF文件

process_directory(directory, keywords)
